﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using iTunesLib;
using Mirage;
using System.Xml;
using MirageTest;

namespace AutoPlaylist
{
    public partial class Form1 : Form
    {
        public static iTunesApp itunes = new iTunesAppClass();
        public static IITTrackCollection tracks = itunes.LibraryPlaylist.Tracks;
        public static Dictionary<string, object> itunesdic = new Dictionary<string, object>();
        public static Db db = new Db();
        public Form1()
        {
            InitializeComponent();

            //itunes = new iTunesAppClass();
            // tracks
            //XDocument itunesxml = XDocument.Load(itunes.LibraryXMLPath);
            //XmlTextReader xtr = (new XmlTextReader((itunes.LibraryXMLPath)));
            //xtr.readtt();
             //iTunesUtil.readDictionary(xtr);
            //db  = new Db();
            using (XmlTextReader reader = new XmlTextReader((itunes.LibraryXMLPath)))
            {
                // Skip to <plist>.
                while (reader.Read() && (
                    reader.NodeType != XmlNodeType.Element ||
                    reader.Name != "plist"))
                {
                    string s = reader.Name;
                }

                // Skip to <dict>.
                while (reader.Read() && (
                    reader.NodeType != XmlNodeType.Element ||
                    reader.Name != "dict"))
                {
                    string s = reader.Name;
                }
                while (reader.Read() && (
                   reader.NodeType != XmlNodeType.Element ||
                   reader.Name != "dict"))
                {
                    string s = reader.Name;
                }

                // Create and populate the dictinary.
                itunesdic = iTunesUtil.readDictionary(reader);
            }
            itunes.OnDatabaseChangedEvent += new _IiTunesEvents_OnDatabaseChangedEventEventHandler(itunes_OnDatabaseChangedEvent);
        }

        void itunes_OnDatabaseChangedEvent(object deletedObjectIDs, object changedObjectIDs)
        {
            //CreateNewAutoPlaylist();
        }

        private void CreateNewAutoPlaylist()
        {
            foreach (IITSource source in itunes.Sources)
            {
                foreach (IITPlaylist playlist in source.Playlists)
                {
                    if (playlist.Name.Contains("Mirage_Auto"))
                    {
                        int[] trackids = new int[playlist.Tracks.Count];
                        List<int> tidlist = new List<int>();
                        int[] newtracks;
                        foreach (IITTrack t in playlist.Tracks)
                        {
                            if (db.GetTrack(t.TrackDatabaseID) == null)
                            {
                                Console.WriteLine("Analyzing: " + ((Dictionary<string, object>)itunesdic[t.TrackDatabaseID.ToString()])["Location"]);
                                Scms scms = Mir.Analyze(((Dictionary<string, object>)itunesdic[t.TrackDatabaseID.ToString()])["Location"].ToString().Remove(0, 17));
                                if (scms != null)
                                    db.AddTrack(t.TrackDatabaseID, scms);
                                Console.WriteLine("Analysis of: " + ((Dictionary<string, object>)itunesdic[t.TrackDatabaseID.ToString()])["Location"] + " complete");
                            }
                            tidlist.Add(t.TrackDatabaseID);

                        }
                        tidlist.CopyTo(trackids);
                        newtracks = Mir.SimilarTracks(trackids, null, db);
                        IITUserPlaylist neplay = (IITUserPlaylist)itunes.CreatePlaylist("Mirage_Results - " + playlist.Name.Substring(12));
                        foreach (int trackdbid in newtracks)
                        {
                            foreach (IITTrack o in itunes.LibraryPlaylist.Tracks)
                            {
                                object newt = o;
                                if(o.TrackDatabaseID == trackdbid)
                                    neplay.AddTrack(ref newt);
                            }
                            
                           
                        }

                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (IITTrack track in tracks)
            {

                Console.WriteLine("Analyzing: " + ((Dictionary<string, object>)itunesdic[track.TrackDatabaseID.ToString()])["Location"]);
                Scms scms = Mir.Analyze(((Dictionary<string, object>)itunesdic[track.TrackDatabaseID.ToString()])["Location"].ToString().Remove(0, 17));
                if (scms != null)
                    db.AddTrack(track.TrackDatabaseID, scms);
                Console.WriteLine("Analysis of: " + ((Dictionary<string, object>)itunesdic[track.TrackDatabaseID.ToString()])["Location"] + " complete");

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            CreateNewAutoPlaylist();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            itunes.CreatePlaylist("Mirage_Auto - Default");
        }

    }
}
